package com.diy.sigmund.diycommon.test.controller.excel.easyexcel.register;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.diy.sigmund.diycommon.test.controller.excel.easyexcel.ReqCustomerDailyImport;
import com.diy.sigmund.diycommon.util.json.JacksonUtil;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ylm-sigmund
 * @since 2021/9/16 17:14
 */
public class CustomerDailyImportListener extends AnalysisEventListener<ReqCustomerDailyImport> {

    List<String> misCodes = Lists.newArrayList();
    Logger log = LoggerFactory.getLogger(CustomerDailyImportListener.class);

    /**
     * 这里会一行行的返回头
     */
    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        Integer approximateTotalRowNumber = context.readSheetHolder().getApproximateTotalRowNumber();
        log.info("总条数：{}", approximateTotalRowNumber);
        // 解析到一条头数据:{"0":{"type":"STRING","stringValue":"额度利率导入模板表","formula":false,"dataFormat":0,"dataFormatString":"General"}}
        log.info("解析到一条头数据:{}", JacksonUtil.toJson(headMap));
        context.readWorkbookHolder().setIgnoreEmptyRow(false);
    }

    /**
     * 每解析一行，回调该方法
     */
    @Override
    public void invoke(ReqCustomerDailyImport data, AnalysisContext context) {
        log.info("data:{}", JacksonUtil.toJson(data));
        String misCode = data.getMisCode();
        if (StringUtils.isEmpty(misCode)) {
            data.setDesc("该单元格数据为空");
            // throw new RuntimeException(String.format("第%s行MIS编码为空，请核实", context.readRowHolder().getRowIndex() + 1));
        }
        if (misCodes.contains(misCode)) {
            throw new RuntimeException(String.format("第%s行MIS编码已重复，请核实", context.readRowHolder().getRowIndex() + 1));
        } else {
            misCodes.add(misCode);
        }
    }

    @Override
    public boolean hasNext(AnalysisContext context) {
        // if (RowTypeEnum.EMPTY.equals(context.readRowHolder().getRowType())){
        //     doAfterAllAnalysed(context);
        //     return false;
        // }
        return super.hasNext(context);
    }

    /**
     * 出现异常回调
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        // ExcelDataConvertException:当数据转换异常的时候，会抛出该异常，此处可以得知第几行，第几列的数据
        // 也可以用变量存储起来，最后统一提示
        if (exception instanceof ExcelDataConvertException) {
            int columnIndex = ((ExcelDataConvertException) exception).getColumnIndex() + 1;
            int rowIndex = ((ExcelDataConvertException) exception).getRowIndex() + 1;
            String message = "第" + rowIndex + "行，第" + columnIndex + "列" + "数据格式有误，请核实";
            throw new RuntimeException(message);
        } else if (exception instanceof RuntimeException) {
            throw exception;
        } else {
            super.onException(exception, context);
        }
    }

    /**
     * 解析完后全部回调
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        misCodes.clear();
    }
}
